﻿using System;
using System.Collections.Generic;
using System.Text;
using ShootSearch.Core;
using System.Diagnostics;

namespace ShootSearch.Parsers
{
    public class HtmlFilter:IFilter
    {
        public HtmlFilter()
        {
            Extensions = new List<string>();
            ContentType = new List<string>();

            ContentType.Add("text/html");
            ContentType.Add("text/xhtml");

        }

        #region IFilter Members

        public List<string> Extensions { get; protected set; }

        public List<string> ContentType { get; protected set; }

        public bool Parse(string url, Encoding encoding, string contentType, byte[] content, out object doc)
        {
            ObjectItem item = new ObjectItem();
            item.Url = url;
            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
            
            #region Load Html
            try
            {
                htmlDoc.LoadHtml(encoding.GetString(content));

            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
            }

            if (htmlDoc == null || htmlDoc.DocumentNode == null)
            {
                doc = null;
                return false;
            } 
            #endregion

            //Get title
            HtmlAgilityPack.HtmlNode titleNode = htmlDoc.DocumentNode.SelectSingleNode("/html/head/title");
            if (titleNode != null)
            {
                item.Add("Title", titleNode.InnerText);
            }
            else
            {
                item.Add("Title", url);
            }
            //Get content
            item.Add("Content", htmlDoc.DocumentNode.InnerText);
            item.Add("DateTime", DateTime.Now.ToString());

            doc = item;

            return true;
            
        }

        #endregion

        #region IConfigable Members

        public ShootSearch.Core.SpiderManConfig Config
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
